package com.zhiqiang.gameserver.nettydemo.demo;

import com.zhiqiang.gameserver.entity.ZQGameServerEntity;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Map;

/**
 * @author YAO
 */
public class LoginServerHandler  extends ChannelInboundHandlerAdapter{
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        Map<String,Object> in = (Map<String,Object>)msg;
//        int[] header = (int[]) in.get("header");
//        userInfo.ReqUserLogin body = (userInfo.ReqUserLogin)in.get("body");
        System.err.println("receive message:");
        ZQGameServerEntity zqGameServerEntity = (ZQGameServerEntity)msg;
        userInfo.ReqUserLogin info = userInfo.ReqUserLogin.parseFrom(zqGameServerEntity.getData());
        System.err.println("body:"+info.toString());

//        System.err.println("body:"+body.toString());
//        userInfo.RespUserLogin.Builder builder = userInfo.RespUserLogin.newBuilder();
//        builder.setAccount1(1);
//        builder.setAccount2(2);
//        builder.setAccount3(3);
//        builder.setAccount4(4);
//        builder.setAccount5(5);
//        builder.setAccount6(6);
//        ctx.writeAndFlush(builder.build());

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("来自"+ctx.channel().remoteAddress()+"的客户端已连接");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("来自"+ctx.channel().remoteAddress()+"的客户端已掉线");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
